home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / prep4gif < prev    next >
Encoding:
Text File  |  2000-03-28  |  3.2 KB  |  113 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5.  
  6. use Gimp qw(:auto N_ __);
  7. use Gimp::Fu;
  8.  
  9. # This script requires a Gimp version >= 0.96 (I haven't checked - ymmv)
  10. # small changes by Marc Lehmann <pcg@goof.com>
  11.  
  12. # prep4gif.pl
  13. # by Seth Burgess <sjburges@gimp.org>
  14. # June 29, 1998
  15. #
  16. # This perl plug-in prepares a multilayer RGB image for use as a
  17. # transparent gif.  To use this prpoerly, you want to have something
  18. # close to the intended background as the bottom layer.  If convert
  19. # to indexed is not selected, the bottom two options are unused.
  20. #
  21. # TODO: Write a nicer GUI than Gimp::Fu provides (learn some gtk)
  22. #       Anything else that seems useful
  23.  
  24. # Gimp::set_trace(TRACE_ALL);
  25.  
  26. sub prep {
  27.     my ($img, $drawable, $threshold, $growth, $index, $dither, $colors) = @_;
  28.  
  29. # Duplicate this image, and work on the duplicate for the rest of the
  30. # procedure.
  31.     my $out = gimp_channel_ops_duplicate($img);
  32.  
  33. # @layers is the ordered list, from top to bottom, of all layers in the
  34. # duplicated image.  To find length of the list, use $#layers
  35.     my @layers = gimp_image_get_layers($out);
  36.  
  37. # if there's not enough layers, abort.
  38.     if ($#layers <= 0) {
  39.         gimp_message(__"You need at least 2 layers to perform prep4gif");
  40.         return;
  41.         }
  42.  
  43. # Show the image early - this makes debugging a breeze
  44.     my $newdisplay = gimp_display_new($out);
  45.  
  46. # Hide the bottom layer, so it doesn't get into the merge visible later.
  47.  
  48.     my $bottomlayer = $layers[$#layers];
  49.     gimp_layer_set_visible($bottomlayer, 0);
  50.     gimp_layer_add_alpha($bottomlayer);
  51.  
  52. # NOTE TO PERL NEWBIES - 'my' variables should be declared in their outermost
  53. # scope - if defined inside the if statement, will disappear to program.
  54.  
  55.     my $foreground;
  56.  
  57.     if ($#layers > 1) {
  58.         $foreground = gimp_image_merge_visible_layers($out, 0);
  59.     }
  60.     else {
  61.         $foreground = $layers[0];
  62.     };
  63.  
  64.     my $layer_mask = gimp_layer_create_mask($foreground,2);
  65.     gimp_image_add_layer_mask ($out, $foreground, $layer_mask);
  66.     gimp_threshold($layer_mask,$threshold,255);
  67.  
  68. # Transfer layer mask to selection, and grow the selection
  69.     gimp_selection_layer_alpha($foreground);
  70.     gimp_selection_grow($out,$growth);
  71.  
  72. # Apply this selection to the background
  73.     gimp_layer_set_visible($bottomlayer, 1);
  74.     gimp_image_set_active_layer($out, $bottomlayer);
  75.     gimp_selection_invert($out);
  76.     gimp_edit_cut($bottomlayer);
  77.  
  78. # Clean up after yourself
  79.     gimp_image_remove_layer_mask($out, $foreground, 1);
  80.     my $outlayer = gimp_image_merge_visible_layers($out,0);
  81.  
  82. # Convert to indexed
  83.     if ($index) {
  84.         gimp_convert_indexed($out,1, MAKE_PALETTE, $colors, $dither, 1, "");
  85.         }
  86.  
  87. # Show all the changes.
  88.     gimp_displays_flush();
  89.  
  90.     ();
  91.     }
  92.  
  93. register
  94.     "prep4gif",
  95.     "Prep for gif",
  96.     "Make the image a small-cut-out of the intended background, so your transparent text doesn't look blocky.",
  97.     "Seth Burgess",
  98.     "Seth Burgess <sjburges\@gimp.org>",
  99.     "2-15-98",
  100.     N_"<Image>/Filters/Web/Prepare for GIF...",
  101.     "RGB*",
  102.     [
  103.      [PF_INT32, "lower_threshold", "Lower Alpha Threshold", 64],
  104.      [PF_INT32, "growth", "How Much growth for safety ",1],
  105.      [PF_TOGGLE, "convert_to_indexed", "Convert Image to indexed", 0],
  106.      [PF_TOGGLE, "dither", "Floyd-Steinberg Dithering?", 1],
  107.      [PF_INT32, "colors", "Colors to quantize to", "255"],
  108.     ],
  109.     \&prep;
  110.  
  111. exit main;
  112.  
  113.